﻿using System;
using System.Configuration;
using System.Xml;

namespace Daneas.Utility.Universals
{
    
    /// <summary>
    /// web.config操作类
    /// Copyright (C) Maticsoft
    /// </summary>
    public sealed class ConfigHelper
    {
        private static string xmlpath = ("../printapp/data/xml/customerconfig.xml");
        /// <summary>
        /// 读取自定义配置
        /// </summary>
        /// <param name="key"></param>
        /// <param name="xmlpath">xml的位置</param>
        /// <returns></returns>
        public static string GetCustomerConfig(string key)
        {
            xmlpath =System.Web.HttpContext.Current.Server.MapPath(xmlpath);

            XmlDocument xml = new XmlDocument();

            xml.LoadXml(xmlpath);

            if (!xml.HasChildNodes)
            {
                return "";
            }

            XmlNode root = xml.FirstChild;

            foreach (XmlNode node in root)
            {
                if (node.Name == key)
                {
                    return node.Value;
                }
            }

            return "";
        }

        /// <summary>
        /// 得到AppSettings中的配置字符串信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConfigString(string key)
        {
            string CacheKey = "AppSettings-" + key;
            object objModel = DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = ConfigurationManager.AppSettings[key];
                    if (objModel != null)
                    {
                        DataCache.SetCache(CacheKey, objModel, TimeSpan.Zero);
                    }
                }
                catch
                { }
            }
            return objModel.ToString();
        }

        /// <summary>
        /// 得到AppSettings中的配置Bool信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool GetConfigBool(string key)
        {
            bool result = false;
            string cfgVal = GetConfigString(key);
            if (null != cfgVal && string.Empty != cfgVal)
            {
                try
                {
                    result = bool.Parse(cfgVal);
                }
                catch (FormatException)
                {
                    // Ignore format exceptions.
                }
            }
            return result;
        }

        /// <summary>
        /// 得到AppSettings中的配置Decimal信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static decimal GetConfigDecimal(string key)
        {
            decimal result = 0;
            string cfgVal = GetConfigString(key);
            if (null != cfgVal && string.Empty != cfgVal)
            {
                try
                {
                    result = decimal.Parse(cfgVal);
                }
                catch (FormatException)
                {
                    // Ignore format exceptions.
                }
            }

            return result;
        }

        /// <summary>
        /// 得到AppSettings中的配置int信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int GetConfigInt(string key)
        {
            int result = 0;
            string cfgVal = GetConfigString(key);
            if (null != cfgVal && string.Empty != cfgVal)
            {
                try
                {
                    result = int.Parse(cfgVal);
                }
                catch (FormatException)
                {
                    // Ignore format exceptions.
                }
            }

            return result;
        }

        /// <summary>
        /// 加密解密web.config,加密appsetting和connectionstring
        /// </summary>
        /// <param name="des">true:加密，false:解密</param>
        public static void SaveWebConfig(bool des)
        {
            Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");

            ConfigurationSection connStr = config.GetSection("connectionStrings");

            if (connStr == null)
            {
                return;
            }

            if (des && !connStr.SectionInformation.IsProtected)
            {
                connStr.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
            }
            else if (!des && connStr.SectionInformation.IsProtected)
            {
                connStr.SectionInformation.UnprotectSection();
            }

            config.Save();
        }
    }
}
